﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;

namespace System.Erp.Runtime
{
    /// <summary>
    ///表示为一个工作空间 
    /// </summary>
    [Serializable]
    public sealed class Workspace : ConstString<Workspace>
    {
        private static readonly ConcurrentDictionary<string, Workspace> _dict;

        static Workspace()
        {
            _global = new Workspace(string.Empty);
            _dict = new ConcurrentDictionary<string, Workspace>();
            _dict.TryAdd(string.Empty, _global);
        }

        private Workspace(string workspace)
            :base(workspace)
        {}

        /// <summary>
        /// 获取一个指定字符串对应的Workspace对象
        /// </summary>
        /// <param name="strWorkspace">工作空间字符串</param>
        /// <returns>对应的Workspace实例</returns>
        public static Workspace GetWorkspace(string strWorkspace)
        {
            strWorkspace = strWorkspace ?? string.Empty;
            return _dict.GetOrAdd(strWorkspace, (k) => new Workspace(k));
        }

        /// <summary>
        /// 返回是否是默认工作空间
        /// </summary>
        public bool IsGlobal
        {
            get { return this.StringValue == string.Empty; }
        }

        private readonly static Workspace _global;
        /// <summary>
        /// 返回全局的工作空间
        /// </summary>
        public static Workspace Global
        {
            get { return _global; }
        }

        #region 相等判断处理
        /// <summary>
        /// 返回两个工作空间是否相等
        /// </summary>
        /// <param name="a">要判断的对象</param>
        /// <param name="b">要判断的对象</param>
        /// <returns>如果对象相等返回true，否则返回false</returns>
        public static bool operator ==(Workspace a, Workspace b)
        {
            return Equals(a, b);
        }

        /// <summary>
        /// 返回两个工作空间是否不相等
        /// </summary>
        /// <param name="a">要判断的对象</param>
        /// <param name="b">要判断的对象</param>
        /// <returns>如果对象不相等返回true，否则返回false</returns>
        public static bool operator !=(Workspace a, Workspace b)
        {
            return !Equals(a, b);
        }

        /// <summary>
        /// 自定义的相等判断
        /// </summary>
        /// <param name="obj">要判断相等的对象</param>
        /// <returns>返回是否相等。</returns>
        public override bool Equals(object obj)
        {
            Workspace other = obj as Workspace;
            return (other == null) ? false : Equals(this, other);
        }

        /// <summary>
        /// 返回此对象的Hashcode，为类型字符串的hashcode.
        /// </summary>
        /// <returns>Hashcode的值。</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// 支持从字符串隐式转换为此工作空间
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>对应的工作空间</returns>
        public static implicit operator Workspace(string str)
        {
            return GetWorkspace(str);
        }
        #endregion
    }
}
